
package com.edocbase.desktop.maildriver;

import com.edocbase.diary.job.IJobProgressListener;
import com.edocbase.diary.job.JobManager;
import com.edocbase.diary.job.JobProgressEvent;
import com.edocbase.diary.job.JobWorker;

class CommandJobWorker extends JobWorker implements IJobProgressListener{
	AbstractDriverCommand cmd = null;
	DriverWorker driverWorker = null;
	boolean send = false;
	CommandJobWorker(DriverWorker driverWorker,boolean send) {
		super();
		this.driverWorker = driverWorker;
		group =JobManager.USER_GROUP;
		this.send = send;
	}

	@Override
	protected void execute() throws Exception {
		if (cmd == null){
			if (send){
				cmd = driverWorker.getNextSendCmd();
			}else{
				cmd = driverWorker.getNextCmd();
			}
		}
		while (cmd != null && (!stop)){
			cmd.execute(this);
			if (send){
				cmd = driverWorker.getNextSendCmd();
			}else{
				cmd = driverWorker.getNextCmd();
			}
		}
//		if (send){
//			driverWorker.driver.transportClose();
//		}else{
//			driverWorker.driver.closeInbox();
//		}
	}
	
	@Override
	public void stop() {
		super.stop();
		if (cmd == null){
			return;
		}
		cmd.stop();
		cmd = null;
	}

	@Override
	public void changed(JobProgressEvent event) {
		event.setSource(this);
		fireProgressChangedEvent(event);
	}

	@Override
	public void completed(JobProgressEvent event) {
		event.setSource(this);
		fireProgressCompletedEvent(event);
	}

	@Override
	public void textChanged(JobProgressEvent event) {
		event.setSource(this);
		setText(event.text);
		fireProgressTextChangedEvent(event);
	}
	
}
